//
//  ScanPayViewController.m
//  HuLaQuan
//
//  Created by hok on 1/25/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ScanPayViewController.h"

#import "ZCTradeView.h"

#import "GetUserAccountInfoEngine.h"
@interface ScanPayViewController ()<ZCTradeViewDelegate>

@property (nonatomic,strong) ZCTradeView *tradeView;
@property (nonatomic,strong) GetUserAccountInfoEngine *getUserAccountInfoEngine;
@end

@implementation ScanPayViewController
- (GetUserAccountInfoEngine *)getUserAccountInfoEngine
{
    if (!_getUserAccountInfoEngine) {
        _getUserAccountInfoEngine = [[GetUserAccountInfoEngine alloc] init];
    }
    return _getUserAccountInfoEngine;
}
- (ScanPayEngine *)scanPayEngine
{
    if (!_scanPayEngine) {
        _scanPayEngine = [[ScanPayEngine alloc] init];
    }
    return _scanPayEngine;
}
- (void)setupChildControllerUI
{
    self.title = @"扫码支付";
    
    self.view.backgroundColor = [UIColor clearColor];
}


- (IBAction)tapCloseButton:(id)sender {
    UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"是否放弃扫码支付操作" andBlock:^(UIAlertAction *action ) {
        if ([action.title isEqualToString:@"好的"]) {
            [self dismissViewControllerAnimated:YES completion:^{if ([self.delegate respondsToSelector:@selector(cancelBtnClick:)]) {
                [self.delegate cancelBtnClick:YES];
            }}];
        }
        
    }  type:@"confirmAndCancel"];
    [self presentViewController:x animated:YES completion:nil];
}


/**
 *  点击支付按钮
 */
-(void)tapPayButton
{
    
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    
   // NSLog(@"%@ %@",self.cardListInfo[@"cardList"],[self.cardListInfo[@"cardList"] firstObject]);
    [self.scanPayEngine getPaymentInfo:[self.cardListInfo[@"cardList"] firstObject]];

    

}
/*-(NSString *)finish:(NSString *)pwd{
    NSLog(@"haha %@",pwd);
    
    // [self.passwordAndInfos setObject:pwd forKey:@"checkpassword"];
    
    
    return pwd;
}
-(NSString *)finish:(NSString *)pwd confirm:(BOOL)isConfirm
{
    [self.tradeView cancle];
    
   // self.shadowView.hidden = NO;
   // self.finishView.hidden = NO;
    return pwd;
}*/


#pragma mark - CallBack

- (void)getScanPaymentInfoFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        
        NSLog(@"支付信息确认成功");
        [self dismissViewControllerAnimated:YES completion:^{if ([self.delegate respondsToSelector:@selector(okBtnClick:)]) {
            [self.delegate okBtnClick:YES];
        }}];
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}

#pragma mark - Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getScanPaymentInfoFinish:) name:@"GetScanPaymentInfoFinish" object:nil];
    
    [self updateUI];
   
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetScanPaymentInfoFinish" object:nil];
 
}

- (void)updateUI
{
    self.showPayment.text = [NSString stringWithFormat:@"¥%@",ServerMoneyFloatToShow(self.scanPayEngine.orderInfo[@"payAmt"])] ;
    self.payName.text = self.scanPayEngine.orderInfo[@"resName"];
    
    self.userCardList = self.cardListInfo[@"cardList"];
    
    NSLog(@"%@",self.userCardList);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
